﻿
Public Class EwinSyncService
    Private log As ActivityLogger
    Private mhl As MySqlHandler
    Private whl As IWinccHandler

    Sub New(ByRef cmhl As MySqlHandler, ByRef cwhl As IWinccHandler)
        ActiviyLogger = ActivityLogger.getInstance
        MySqlDbHandler = cmhl
        WinHandler = cwhl
    End Sub

    Public Property ActiviyLogger As ActivityLogger
        Get
            Return log
        End Get
        Set(ByVal value As ActivityLogger)
            log = value
        End Set
    End Property

    Public Property MySqlDbHandler As MySqlHandler
        Get
            Return mhl
        End Get
        Set(ByVal value As MySqlHandler)
            mhl = value
        End Set
    End Property

    Public Property WinHandler As IWinccHandler
        Get
            Return whl
        End Get
        Set(ByVal value As IWinccHandler)
            whl = value
        End Set
    End Property

    Public Function performImport(ByVal start As DateTime, ByVal endt As DateTime) As Boolean
        Dim tm As ewindt.DtTableMappingDataTable = mhl.getTablesMapping()
        Dim cm As ewindt.DtColumnMappingDataTable = mhl.getColumnMapping
        'process tag
        processTag(start, endt, tm, cm)
        'process alarm
        Return True
    End Function
    Private Function processTag(ByVal start As DateTime, ByVal endt As DateTime, ByVal tm As ewindt.DtTableMappingDataTable, ByVal cm As ewindt.DtColumnMappingDataTable) As Boolean
        Dim tr() As ewindt.DtTableMappingRow = tm.Select(tm.wincctablenameColumn.ColumnName + "='" + WinccConfig.ARCHIVE + "'")
        For Each r As ewindt.DtTableMappingRow In tr
            Dim crs() As ewindt.DtColumnMappingRow = cm.Select(cm.idtablemappingColumn.ColumnName + "=" + r.id.ToString)
            Dim wsort As New WinCcDataSorter
            Dim tagmap As New ArrayList
            For Each cr As ewindt.DtColumnMappingRow In crs
                Dim col As New WcTagValue(cr.wincolid, cr.wincolname)
                wsort.addCol(col)
                tagmap.Add(col)
            Next
            Dim res As ewindt.DtProcessValueDataTable = Me.whl.getTagValue(start, endt, tagmap)
            wsort.setvalue(res)
            Me.mhl.recordTagValue(r, crs, wsort)
        Next
        Return True
    End Function

End Class
